package cn.xscrum.vtsp.controller;

import cn.xscrum.common.annotation.Log;
import cn.xscrum.common.core.controller.BaseController;
import cn.xscrum.common.core.domain.AjaxResult;
import cn.xscrum.common.core.page.TableDataInfo;
import cn.xscrum.common.enums.BusinessType;
import cn.xscrum.vtsp.domain.*;
import cn.xscrum.vtsp.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 入队申请信息Controller
 *
 * @author Zxing
 * @date 2021-10-01
 *
 */
@Controller
@RequestMapping("/vtsp/apply")
public class VtspApplyController extends BaseController {
    //静态资源前缀
    private String prefix = "vtsp/apply";

    @Autowired
    private IVtspApplyService vtspApplyService;

    @Autowired
    private IVtspSectorService vtspSectorService;

    @Autowired
    private IVtspAcademyService vtspAcademyService;

    /**
     * 返回静态资源templates中的"/vtsp/apply/apply"视图
     */
    @RequiresPermissions("vtsp:apply:view")
    @GetMapping()
    public String volunteer(Model model) {
        //查询部门表的信息
        List<VtspSector> sectorList = vtspSectorService.selectVtspSectorList(new VtspSector());
        //查询学院表的信息
        List<VtspAcademy> academyList = vtspAcademyService.selectVtspAcademyList(new VtspAcademy());
        //给viewModel对象添加sectorList属性
        model.addAttribute("sectorList", sectorList);
        //给viewModel对象添加academyList属性
        model.addAttribute("academyList", academyList);
        return prefix + "/apply";
    }

    /**
     * 按vtspVolunteer对象 条件查询志愿者信息，返回列表(Mapper.xml中动态SQL实现)
     */
    @RequiresPermissions("vtsp:apply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VtspVolunteer vtspVolunteer) {
        startPage();
        //  因为传来的结束时间是yyyy-MM-dd 00:00:00, 将结束时间往后挪至次日前一秒
        if(vtspVolunteer.getEndDate() != null) {
            //  Date -> 时间戳
            long timeStamp = vtspVolunteer.getEndDate().getTime();
            //  时间戳 + (一天的秒数 - 1) * 1000
            timeStamp += (24 * 60 * 60 - 1) * 1000;
            //  时间戳 -> Date
            vtspVolunteer.setEndDate(new Date(timeStamp));
        }
        //  根据vtspVolunteer对象的属性进行条件查询
        List<VtspVolunteer> list = vtspApplyService.selectVtspVolunteerList(vtspVolunteer);
        return getDataTable(list);
    }

    /**
     * 跳转修改志愿者信息iframe页面
     */
    @GetMapping("/edit/{volunteerId}")
    public String edit(@PathVariable("volunteerId") Long volunteerId, Model model, ModelMap mmap) {
        //按Id查询志愿者信息
        VtspVolunteer vtspVolunteer = vtspApplyService.selectVtspVolunteerById(volunteerId);
        //取到全部的部门表信息
        List<VtspSector> sectorList = vtspSectorService.selectVtspSectorList(new VtspSector());
        //添加MVC返回数据
        mmap.put("vtspVolunteer", vtspVolunteer);
        //添加MVC返回数据
        model.addAttribute("sectorList", sectorList);
        return prefix + "/edit";
    }

    /**
     * 修改保存志愿者信息
     */
    @RequiresPermissions("vtsp:apply:edit")
    @Log(title = "志愿者信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VtspVolunteer vtspVolunteer) {
        //调用Service提交修改
        return toAjax(vtspApplyService.updateVtspVolunteer(vtspVolunteer));
    }

    /**
     * 批量拒绝入队申请
     * memberState 1 -> 3, 并且附带拒绝信息（理由和描述拼接）存储在apply_info中
     */
    @RequiresPermissions("vtsp:apply:refuseAll")
    @Log(title = "志愿者信息", businessType = BusinessType.UPDATE)
    @PostMapping("/refuseAll")
    @ResponseBody
    public AjaxResult refuseAll(String ids, String applyInfo) {
        //调用Service提交信息变更
        return toAjax(vtspApplyService.refuseVtspVolunteerByIds(ids, applyInfo));
    }

    /**
     * 批量同意入队申请
     * memberState 1 -> 6, 并且将apply_info修改为 "申请通过"
     */
    @RequiresPermissions("vtsp:apply:acceptAll")
    @Log(title = "志愿者信息", businessType = BusinessType.UPDATE)
    @PostMapping("/acceptAll")
    @ResponseBody
    public AjaxResult acceptAll(String ids) {
        //调用Service提交信息变更
        return toAjax(vtspApplyService.acceptVtspVolunteerByIds(ids));
    }
}
